<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
use think\Request;

//use \think\View;
//        $a=Db::query('select * from ceshi where id=?',[1]); //原生查询数据
//        $a=Db::table('ceshi')->where('id','2')->select();//查询数据
//        $b=Db::execute('insert into ceshi (name) values (?)',['zhang']);
//        $view=new View();
//        return $view->fetch('regisert');
//        var_dump($_POST);
//        $data=[['name'=>'llliq'],
//              ['name'=>'ssss']];
//        Db::table('ceshi')->insertAll($data); //插入数据
//        Db::table('ceshi')->where('id',5)->update(['name'=>'zhangsan']); //更新数据
//        Db::table('ceshi')->where('id','>',5)->delete(); //根据条件删除
//        Db::table('ceshi')->delete([4]);//根据主键删除
//        Db::execute("update ceshi set name='lisi' where id=3");//原生更新数据
//        $b=Db::table('ceshi')->field('id,name')->where('id','>','1')->select(false); //返回sql语句
//        echo $b;
session_start();
class Index extends \think\Controller
{
    public function index()
    {

//        var_dump($_SESSION);
        if (empty($_SESSION)) {
            $uname = "";
        } else {

        $uname = $_SESSION['username'];
    }
        $this->assign('name',"$uname");

       return view('index');
    }
    public function login(){
        return view('login');
    }
    public function login_in(){

            $username = $_POST['username'];
            $password = md5($_POST['password']);
            $sql = Db::table('user')->where('username', "$username")->select();
            if ($sql) {
                if ($password == $sql[0]['password']) {
                    $result['status'] = 1;

                    $_SESSION['username'] = $username;
                    $_SESSION['uid'] = $sql[0]['id'];
                } else {
                    $result['status'] = 0;
                }
            } else {
                $result['status'] = 0;
            }
            echo json_encode($result);

    }
    public  function  loginout(){
        session_unset();
        session_destroy();
        $this->redirect('http://www.yyd.com/index/index');
    }
    public function register(){

//$c=input('param.username'); //获取所有类型传的参数如$_POST/$_GET


        $a='Welcome to register';
        $this->assign('title',"$a");//变量赋值
return $this->fetch('register',['b'=>'注册']); //两种渲染模版赋值的方法
//return view('regisert');
    }
    public function adduser(){
        $date['username']=trim($_POST['username']);
        $date['password']=md5(trim($_POST['password']));
        $date['email']=trim($_POST['email']);
        $date['phone']=trim($_POST['phone']);
        $a=$date['username'];
        $sql=Db::table('user')->where('username',"$a")->select();
        if($sql){
            $c['status']=0;
        }else{
            $c['status']=1;
            Db::table('user')->insert($date);
        }
        echo json_encode($c);
    }
 public function retrieve(){
        $a=input('param.user');
        echo $a;
        return view('retrieve');
 }
 public function vote(){
     if(empty($_SESSION)){
         $this->redirect('http://www.yyd.com/index/login');
     }else {
         $data['name']=$_SESSION['username'];
         $data['uid']=$_SESSION['uid'];
//        var_dump($_SESSION);
         $this->assign('data',$data);
         return view('vote');
     }
 }
    public function coach(){

        if(empty($_SESSION)){
            $this->redirect('http://www.yyd.com/index/login');
        }else {
        $data['name']=$_SESSION['username'];
        $data['uid']=$_SESSION['uid'];
//        var_dump($_SESSION);
        $this->assign('data',$data);

            $select=Db::query('select * from coach');
            $this->assign('coach',$select);

            return view('coach');
        }
    }
  public function upcoach(){
//        var_dump($_POST);
      $coach=$_POST['coach'];
      $zannumber = $_POST['zannumber'];
      $coachid=$_POST['coachid'];
      $data['username'] = $_POST['name'];
      $data['uid'] = $_POST['uid'];
      $data['coach'] = '已赞';
      $uid = $data['uid'];
      $sql = Db::table('zannumber')->where('uid', "$uid")->select();
//         var_dump($sql);
      if ($sql) {
          if ($sql[0]['coach']) {
              $b['status'] = 0;
          } else {
                  Db::table('zannumber')->where('uid', $uid)->update(['coach' => '已赞']);
                  $zannum = $zannumber + 1;
                  $a = Db::table('coach')->where('id', $coachid)->update(['zannumber' => "$zannum"]);
                  if ($a) {
                      $b['status'] = 1;
                  }
          }
//        echo '{"code":"0","msg":"操作成功"}';
      } else {
          Db::table('zannumber')->insert($data);

//        $zannumber1=Db::query('select zannumber from coach where id=1' );
//        $zannumber2=$zannumber1[0]['zannumber'];
          $zannum = $zannumber + 1;
          $a = Db::table('coach')->where('id', $coachid)->update(['zannumber' => "$zannum"]);
          if ($a) {
              $b['status'] = 1;
          } else {
              $b['status'] = 0;
          }

      }
      echo json_encode($b);
  }
    public function player(){
        if(empty($_SESSION)){
            $this->redirect('http://www.yyd.com/index/login');
        }else {
            $data['name']=$_SESSION['username'];
            $data['uid']=$_SESSION['uid'];
//        var_dump($_SESSION);
            $this->assign('data',$data);

            $player = Db::query('select * from player');
//var_dump($coach[0]);
            $this->assign("player", $player);

            return view('player');
        }
    }
    public function upplayer()
    {
//        var_dump($_POST);
        $player = $_POST['player'];
        $zannumber = $_POST['zannumber'];
        $playerid=$_POST['playerid'];
        $data['username'] = $_POST['name'];
        $data['uid'] = $_POST['uid'];
        $data['player'] = '已赞';
        $uid = $data['uid'];
        $sql = Db::table('zannumber')->where('uid', "$uid")->select();
//         var_dump($sql);
        if ($sql) {
            if ($sql[0]['player']) {
                $b['status'] = 0;
            } else {
                    Db::table('zannumber')->where('uid', $uid)->update(['player' => '已赞']);
                    $zannum = $zannumber + 1;
                    $a = Db::table('player')->where('id', $playerid)->update(['zannumber' => "$zannum"]);
                    if ($a) {
                        $b['status'] = 1;
                    }
            }
        } else {
            Db::table('zannumber')->insert($data);
            $zannum = $zannumber + 1;
            $a = Db::table('player')->where('id', $playerid)->update(['zannumber' => "$zannum"]);
            if ($a) {
                $b['status'] = 1;
            } else {
                $b['status'] = 0;
            }

        }
        echo json_encode($b);
    }
    public function team(){
        if(empty($_SESSION)){
            $this->redirect('http://www.yyd.com/index/login');
        }else {
            $data['name']=$_SESSION['username'];
            $data['uid']=$_SESSION['uid'];
//        var_dump($_SESSION);
            $this->assign('data',$data);

            $team = Db::query('select * from team');
//var_dump($coach[0]);
            $this->assign("team", $team);
            return view('team');
        }
    }
    public function upteam()
    {
//        var_dump($_POST);
        $team = $_POST['team'];
        $zannumber = $_POST['zannumber'];
        $teamid=$_POST['teamid'];
        $data['username'] = $_POST['name'];
        $data['uid'] = $_POST['uid'];
        $data['team'] = '已赞';
        $uid = $data['uid'];
        $sql = Db::table('zannumber')->where('uid', "$uid")->select();
//         var_dump($sql);
        if ($sql) {
            if ($sql[0]['team']) {
                $b['status'] = 0;
            } else {
                Db::table('zannumber')->where('uid', $uid)->update(['team' => '已赞']);
                $zannum = $zannumber + 1;
                $a = Db::table('team')->where('id', $teamid)->update(['zannumber' => "$zannum"]);
                if ($a) {
                    $b['status'] = 1;
                }
            }
        } else {
            Db::table('zannumber')->insert($data);
            $zannum = $zannumber + 1;
            $a = Db::table('team')->where('id', $teamid)->update(['zannumber' => "$zannum"]);
            if ($a) {
                $b['status'] = 1;
            } else {
                $b['status'] = 0;
            }

        }
        echo json_encode($b);
    }
    public function cheerteam(){
        if(empty($_SESSION)){
            $this->redirect('http://www.yyd.com/index/login');
        }else {
            $data['name']=$_SESSION['username'];
            $data['uid']=$_SESSION['uid'];
//        var_dump($_SESSION);
            $this->assign('data',$data);

            $cheerteam = Db::query('select * from cheerteam');
//var_dump($coach[0]);
            $this->assign("cheerteam", $cheerteam);
            return view('cheerteam');
        }
    }
    public function upcheerteam()
    {
//        var_dump($_POST);
        $cheerteamid=$_POST['cheerteamid'];
        $cheerteam = $_POST['cheerteam'];
        $zannumber = $_POST['zannumber'];
        $data['username'] = $_POST['name'];
        $data['uid'] = $_POST['uid'];
        $data['cheerteam'] = '已赞';
        $uid = $data['uid'];
        $sql = Db::table('zannumber')->where('uid', "$uid")->select();
//         var_dump($sql);
        if ($sql) {
            if ($sql[0]['cheerteam']) {
                $b['status'] = 0;
            } else {
                Db::table('zannumber')->where('uid', $uid)->update(['cheerteam' => '已赞']);
                $zannum = $zannumber + 1;
                $a = Db::table('cheerteam')->where('id', $cheerteamid)->update(['zannumber' => "$zannum"]);
                if ($a) {
                    $b['status'] = 1;
                }
            }
        } else {
            Db::table('zannumber')->insert($data);
            $zannum = $zannumber + 1;
            $a = Db::table('cheerteam')->where('id', $cheerteamid)->update(['zannumber' => "$zannum"]);
            if ($a) {
                $b['status'] = 1;
            } else {
                $b['status'] = 0;
            }

        }
        echo json_encode($b);
    }
    public function lianxi(){
        $select=Db::query('select * from coach');
        $this->assign('data',$select);

        return view('lianxi');
    }
}


